<?php
namespace app\index\controller;
use \think\Controller;
use \think\View;
use app\index\model\FallbackModel;
use app\index\model\LoginModel;
use app\index\model\BaseModel;


class Fallback extends Controller
{
	public function index()
	{
		$user_info = LoginModel::get_user_info(session('user_no'));
		$this->assign('username',$user_info['username']);
		return $this->fetch("index");
	}

	public function add()
	{
		$name = $_REQUEST['name'];
		$contact = $_REQUEST['contact'];
		$content = $_REQUEST['content'];
		if(!$name){
			return json(array('code'=>'1','msg'=>'请输入您的姓名！'));
		}
		if(!$contact){
			return json(array('code'=>'1','msg'=>'请输入您的联系方式！'));
		}
		if(!$content){
			return json(array('code'=>'1','msg'=>'请输入您的内容！'));
		}
		if(mb_strlen($name)>10){
			return json(array('code'=>'-30001','msg'=>'姓名长度超过最大限制！'));
		}
		if(mb_strlen($contact)>20){
			return json(array('code'=>'-30001','msg'=>'联系方式长度超过最大限制！'));
		}
		if(mb_strlen($content)>250){
			return json(array('code'=>'-30001','msg'=>'内容长度超过最大限制！'));
		}
		$data['user_id'] = session('user_id');
		$data['name'] = $name;
		$data['contact'] = $contact;
		$data['content'] = $content;
		$id = BaseModel::insert('fallback',$data,1);
		if($id){
			return json(array('code'=>0,'msg'=>'提交成功，感谢你的反馈，我们将努力做好最好！'));
		}else{
			return json(array('code'=>'-30002','msg'=>'提交失败，请重试！'));
		}
	}

	public function getList()
	{
		$list = FallbackModel::get_list();
		$page = $list->render();
		$this->assign('list',$list); //	渲染模板输出
		$this->assign('page',$page);
		return	$this->fetch('list');

	}

}